package es.efor.ics.gev.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * Un atributo asociado a un perfil con informacion adicional
 * 
 * @see Perfil
 * @see AtributoMaestro
 */
@Entity
@Table(name = "ATRIBUTOS_PERFILES")
public class AtributoPerfil extends BaseObject implements Serializable,
		AtributoAsignado {

	/**
	 * Embedded composite identifier class that represents the primary key
	 * columns of the many-to-many join table.
	 */
	@Embeddable
	public static class Id implements Serializable {

		private static final long serialVersionUID = -611764296746896828L;

		private Long perfilId;
		private Long atributoId;

		public Id() {
		}

		public Id(Long perfilId, Long atributoId) {
			this.perfilId = perfilId;
			this.atributoId = atributoId;
		}

		@Column(name = "PERFIL_ID")
		public Long getPerfilId() {
			return perfilId;
		}

		public void setPerfilId(Long perfilId) {
			this.perfilId = perfilId;
		}

		@Column(name = "ATRIBUTO_ID")
		public Long getAtributoId() {
			return atributoId;
		}

		public void setAtributoId(Long atributoId) {
			this.atributoId = atributoId;
		}

		@Override
		public boolean equals(Object o) {
			if (o instanceof Id) {
				Id that = (Id) o;
				return this.perfilId.equals(that.perfilId)
						&& this.atributoId.equals(that.atributoId);
			} else {
				return false;
			}
		}

		@Override
		public int hashCode() {
			return perfilId.hashCode() + atributoId.hashCode();
		}

		@Override
		public String toString() {
			return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
					.append("perfilId", this.perfilId).append("atributoId",
							this.atributoId).toString();
		}

	}

	// static data
	private static final long serialVersionUID = 6475010906905474033L;

	// private instance data
	private Id id;
	private Perfil perfil;
	private AtributoMaestro atributo;
	private boolean obligatorio;
	private Integer orden;
	private Integer version;

	/**
	 * Construct an uninitialized <code>AtributoPerfil</code>.
	 */
	protected AtributoPerfil() {
	}

	/**
	 * Construct an initialized <code>AtributoPerfil</code>.
	 * 
	 * @param perfil
	 * @param atributo
	 * @param obligatorio
	 * @param orden
	 */
	public AtributoPerfil(Perfil perfil, AtributoMaestro atributo,
			boolean obligatorio, Integer orden) {
		this.perfil = perfil;
		this.atributo = atributo;
		this.obligatorio = obligatorio;
		this.orden = orden;

		// Set primary key
		this.id = new Id(perfil.getId(), atributo.getId());
	}

	// ********************** Accessor Methods ********************** //

	@EmbeddedId
	public Id getId() {
		return id;
	}

	public void setId(Id id) {
		this.id = id;
	}

	@ManyToOne
	@JoinColumn(name = "PERFIL_ID", insertable = false, updatable = false, nullable = false)
	@org.hibernate.annotations.ForeignKey(name = "FK_ATRIB_PERF_PERFIL_ID")
	public Perfil getPerfil() {
		return perfil;
	}

	public void setPerfil(Perfil perfil) {
		this.perfil = perfil;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ATRIBUTO_ID", insertable = false, updatable = false)
	@org.hibernate.annotations.ForeignKey(name = "FK_ATRIB_PERF_ATRIBUTO_ID")
	public AtributoMaestro getAtributo() {
		return atributo;
	}

	public void setAtributo(AtributoMaestro atributo) {
		this.atributo = atributo;
	}

	@Column(name = "ORDEN", nullable = false)
	public Integer getOrden() {
		return orden;
	}

	public void setOrden(Integer orden) {
		this.orden = orden;
	}

	@Column(name = "OBLIGATORIO")
	public boolean isObligatorio() {
		return obligatorio;
	}

	public void setObligatorio(boolean obligatorio) {
		this.obligatorio = obligatorio;
	}
	
	@Version
	@Column(name = "OBJ_VERSION")
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}
	
	// ********************** Common Methods ********************** //

	@Override
	public int hashCode() {
		return id.hashCode();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		final AtributoPerfil that = (AtributoPerfil) o;
		return id.equals(that.id);
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append(
				"perfil", this.getPerfil()).append("atributo",
				this.getAtributo()).append("obligatorio", this.isObligatorio())
				.append("orden", this.getOrden())
				.toString();
	}

	/**
	 * {@inheritDoc} 
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(Object o) {
		final int BEFORE = -1;
		final int EQUAL = 0;
		final int AFTER = 1;

		AtributoPerfil aThat = (AtributoPerfil) o; 
		// this optimization is usually worthwhile, and can
		// always be added
		if (this == aThat)
			return EQUAL;

		// primitive numbers follow this form
		if (this.orden < aThat.orden)
			return BEFORE;
		if (this.orden > aThat.orden)
			return AFTER;

		// all comparisons have yielded equality
		// verify that compareTo is consistent with equals (optional)
		assert this.equals(aThat) : "compareTo inconsistent with equals.";

		return EQUAL;
	}

}
